#!/bin/bash
set -x
# params 1 repository xxx.com/harbor
# params 2 build_id

# nohup ./build.sh xxx.com/platform $(date +%Y%m%d_%H%M%S) > output.log 2>&1 &

# -Dmaven.compile.fork=true 参数，用以指明使用多线程进行编译；
# -Dmaven.test.skip=true 跳过测试代码的编译命令
# -T 1C 参数，表示使用每个 CPU 核心跑一个工程；


repositories="xxx.com/platform"
tag=$(date +%Y%m%d_%H%M%S)
if [ ! -n "$1" ]; then
  echo "repositories IS NULL"
else
  repositories=$1
  echo "repositories IS $repositories"
fi

if [ ! -n "$2" ]; then
  echo "tag IS NULL"
else
  tag=$2
  echo "tag IS $tag"
fi

mvn clean install -T 1C -Dmaven.test.skip=true -Dmaven.compile.fork=true

git tag "b_"$tag
git push origin "b_"$tag

project_root=$(pwd)

buildProject(){
  echo "dsName $1"
  echo "dsNamespace $2"
  echo "project name $3"
  echo "project path $4"
  dsName=$1
  dsNamespace=$2
  projectName=$3
  projectPath=$4
  kind=$5

  cd ${project_root}/platform-cloud/${projectPath}/${projectName}/target
  docker build -f classes/Dockerfile -t $repositories/${projectName}:$tag .
  docker push $repositories/${projectName}:$tag
  kubectl -n ${dsNamespace} set image ${kind}  ${dsName} ${dsName}=$repositories/${projectName}:$tag

}

buildProject platform-cloud-commons-admin platform-common platform-cloud-commons-admin platform-cloud-commons statefulset

buildProject p-c-s-channel platform-cloud-service platform-cloud-service-channel platform-cloud-service statefulset
buildProject p-c-s-link platform-cloud-service platform-cloud-service-link platform-cloud-service statefulset
buildProject p-c-s-message platform-cloud-service platform-cloud-service-message platform-cloud-service statefulset
buildProject p-c-s-network platform-cloud-service platform-cloud-service-network platform-cloud-service statefulset
buildProject p-c-s-security platform-cloud-service platform-cloud-service-security platform-cloud-service statefulset
buildProject p-c-s-pay platform-cloud-service platform-cloud-service-pay platform-cloud-service statefulset
buildProject p-c-s-user platform-cloud-service platform-cloud-service-user platform-cloud-service statefulset
buildProject p-c-s-data platform-cloud-service platform-cloud-service-data platform-cloud-service statefulset
buildProject p-c-s-task platform-cloud-service platform-cloud-service-task platform-cloud-service statefulset

buildProject p-c-w-auth platform-cloud-web platform-cloud-web-auth platform-cloud-web statefulset
buildProject p-c-w-notice platform-cloud-web platform-cloud-web-notice platform-cloud-web statefulset
buildProject p-c-w-gateway platform-cloud-web platform-cloud-web-gateway platform-cloud-web statefulset
buildProject p-c-w-link platform-cloud-web platform-cloud-web-link platform-cloud-web statefulset
buildProject p-c-w-netty platform-cloud-web platform-cloud-web-netty platform-cloud-web statefulset
buildProject p-c-w-file platform-cloud-web platform-cloud-web-file platform-cloud-web statefulset
buildProject p-c-w-tool platform-cloud-web platform-cloud-web-tool platform-cloud-web statefulset